
import { Op } from 'sequelize'
import Books from '../../decormodel/books'
import { sequelize } from '../../BaseDao'
import { getNoReptItem } from '../../commontypes'
import Pager,{ pagerDecoraotr } from '@/common/PageUtil'
class BookDao {
  static bookDao: BookDao = new BookDao()
  async findBookLstWithPager(curPageNo: string){
    const basePagerSql="select * from books "
    const recTotalNumSql="select count(isbn) from books "
    const countPageField='isbn';
    await this.bookPager(curPageNo,basePagerSql,recTotalNumSql,countPageField);
    return Pager.getcurPageData();
    // return curPageDataList;
  }
  @pagerDecoraotr(sequelize)
  bookPager(curPageNo:string,basePagerSql:string,recTotalNumSql:string,countPageField:string){}
  // async findBookLstWithPager(curPageNo: string):Promise<any[]>{
  //   const firstRecNo = Pager.getFirstRecNoCurPage(curPageNo);
  //   const sql = `select * from books limit ${firstRecNo},${Pager.PageSize}`;
  //   const curPageDataList=(await sequelize.query(sql))[0];
  //   console.log("curPageDataList:",curPageDataList);
  //   const totalRecNumObj = (await sequelize.query('select count(isbn) from books'))[0][0];
  //   const totalRecNum = totalRecNumObj['count(isbn)'];
  //   const totalPageNum = Pager.getTotalPageNum(totalRecNum);
  //   console.log("totalPageNum:", totalPageNum);
  //   return curPageDataList;
  // }
  async findBooksByThirdCtgyId(thirdctgyid: number, offset: number, pageSize: number) {
    let data: any[] = []
    let dataLength: any[]
    if (thirdctgyid == 0) {
      const sql = `select count(*) from books`
      dataLength = (await sequelize.query(sql))[0]
      data = await Books.findAll({
        raw: true,
        limit: pageSize,
        offset,
      })
    } else {
      const sql = `select count(*) from books where thirdctgyid = ${thirdctgyid}`
      dataLength = (await sequelize.query(sql))[0]
      data = await Books.findAll({
        raw: true,
        where: {
          thirdctgyid,
        },
        limit: pageSize,
        offset,
      })
    }
    const paging = {
      pageNum: offset,
      pageSize,
      length: dataLength[0]['count(*)'],
      hasMoreData: false,
    }
    console.log('data', data)
    if (data.length === 0 || offset > dataLength[0]['count(*)']) {
      console.log('hasMoreData')
      paging.hasMoreData = true
    }

    return { data, paging }
  }
  // insert into userinfo(username, psw, birth) values('lisi', '124', '2000/06/07 10:10:02')

  async findBookRisefall(thirdctgyid: number, risefall: string) {
    if (thirdctgyid == 0) {
      return await Books.findAll({
        raw: true,
        order: [['originalprice', risefall]],
      })
    } else {
      return await Books.findAll({
        raw: true,
        order: [['originalprice', risefall]],
        where: {
          thirdctgyid,
        },
      })
    }
  }

  async findBookDetail(ISBN: string) {
    return await Books.findOne({
      raw: true,
      where: {
        ISBN,
      },
    })
  }
  // 自动补全搜索 模糊查询
  async findBooksByAutoCompKeyword(autocompKeyword: string): Promise<Books[]> {
    return await Books.findAll({
      raw: true,
      where: {
        bookname:{
          [Op.like]:`%${autocompKeyword}%`
        }
      },
    })
  }
  // 查询图书当中的出版社
  async findPublisersByAutoCompKey(autocompKeyword: string){
    const books = await Books.findAll({
      attributes:['publishid','publishername'],
      raw: true,
      where: {
        bookname:{
          [Op.like]:`%${autocompKeyword}%`
        }
      },
    })
    return getNoReptItem(books,'publishid');// 对 publishid 进行去重复
  }
  // 通过出版社idlist 查询
  async findBksByPublishIds(publishids: number[]) {
    return await Books.findAll({
      raw: true,
      where: {
        publishid:{
          [Op.in]:publishids
        }
      },
    })
  }

}

export default BookDao.bookDao
